<?php 

function register_my_menu() {
  register_nav_menu('footer-menu',__( 'Footer Menu' ));
}
add_action( 'init', 'register_my_menu' );

function add_custom_meta_boxes() {  
    add_meta_box('wp_custom_attachment', 'ไฟล์แค็ทตาล็อก', 'wp_custom_attachment', 'catalog', 'normal', 'high');  
	add_meta_box('wp_job_field', 'รายละเอียดงาน', 'wp_job_field', '่job', 'normal', 'high');  
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');  


//////PDF attachment



function wp_custom_attachment() {  
    wp_nonce_field(plugin_basename(__FILE__), 'wp_custom_attachment_nonce');
    $html = '<p class="description">';
    $html .= 'อัพโหลดไฟล์แค็ทตาล็อก ในรูปแบบ PDF ที่นี่';
    $html .= '</p>';
    $html .= '<input type="file" id="wp_custom_attachment" name="wp_custom_attachment" value="" size="25">';
    echo $html;
}

add_action('save_post', 'save_custom_meta_data');
function save_custom_meta_data($id) {
    if(!empty($_FILES['wp_custom_attachment']['name'])) {
        $supported_types = array('application/pdf');
        $arr_file_type = wp_check_filetype(basename($_FILES['wp_custom_attachment']['name']));
        $uploaded_type = $arr_file_type['type'];

        if(in_array($uploaded_type, $supported_types)) {
            $upload = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));
            if(isset($upload['error']) && $upload['error'] != 0) {
                wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
            } else {
				add_post_meta($id, 'wp_custom_attachment', $upload);
                update_post_meta($id, 'wp_custom_attachment', $upload);
            }
        }
        else {
            wp_die("กรุณาอัพโหลดเฉพาะ ไฟล์ PDF เท่านั้น.");
        }
    }
}

function update_edit_form() {
    echo ' enctype="multipart/form-data"';
}
add_action('post_edit_form_tag', 'update_edit_form');

//////JOB
$keyArray = array (
	"jobfield-position"  => array("description" => "ตำแหน่ง"),
    "jobfield-requirements" => array("description" => "คุณสมบัติผู้สมัคร"),
    "jobfield-edu" => array("description" => "ระดับการศึกษา"),
	"jobfield-subEdu" => array("description" => "สาขาวิชา"),
	"jobfield-gpa" => array("description" => "เกรดเฉลี่ย"),
	"jobfield-age" => array("description" => "อายุ"),
	"jobfield-sex" => array("description" => "เพศ"),
	"jobfield-region" => array("description" => "อาศัยอยู่"),
	"jobfield-jobtype" => array("description" => "ลักษณะงาน"),
	"jobfield-jobdetail" => array("description" => "รายละเอียดงาน"),
	"jobfield-available" => array("description" => "อัตรา"),
	"jobfield-salary" => array("description" => "เงินเดือน"),
	"jobfield-benefit" => array("description" => "สวัสดิการ")
);
function wp_job_field() {  
    $html = '<table>';
	global $keyArray;
	$id = get_the_ID();
	foreach ($keyArray as $key => $value) {
		 $html .='<tr>';
		 $html .='<td style="width:150px" align="right"><span style="font-weight:bold;">'.wp_get_job_field_desc($key).' :</span></td>';
		 $defaultValue = get_post_meta($id, $key, $single=true);
		 $html .='<td style="width:550px"><input type="text" id="'.$key.'" name="'.$key.'" value="'.$defaultValue.'" size="25"></td></tr>';
	}
    $html .= '</table>';
   
    echo $html;
}
function wp_get_job_field_desc($key){
	global $keyArray;
	return $keyArray[$key]["description"];
}

function wp_sub_bullet($str){
	$order   = array("- ");
	$replace = '<br />- ';

	// Processes \r\n's first so they aren't converted twice.
	$newstr = str_replace($order, $replace, $str);
	return $newstr;
}
function wp_save_job_field($id) { 
	global $keyArray;
	foreach ($keyArray as $key => $value) {
		echo $_POST[$key];
		$input = isset($_POST[$key]) ? $_POST[$key] : '';
		delete_post_meta($id, $key);
		add_post_meta($id, $key, $input);
		
	}
}
add_action('save_post', 'wp_save_job_field');
?>